/**
 * Project Name:springBoot-serviceImpl
 * File Name:FtpServiceImp.java
 * Package Name:com.libra.serviceImp
 * Date:2018年11月6日下午4:36:08
 * Copyright (c) 2018, wangzhch All Rights Reserved.
 *
*/

package com.libra.serviceImp;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.libra.model.ftp.FtpResponse;
import com.libra.model.vo.ResponseData;
import com.libra.service.FtpService;
import com.libra.service.constans.FtpConstans;
import com.libra.service.utils.FtpUtils;
import com.libra.service.utils.PathUtil;

/**
 * ClassName:FtpServiceImp <br/>
 * Date:     2018年11月6日 下午4:36:08 <br/>
 * @author   wangzhch
 * @version  
 * @since    JDK 1.8
 * @see 	 
 */
@Service
public class FtpServiceImp implements FtpService{

	private Logger log = LoggerFactory.getLogger(FtpServiceImp.class);
	
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
	/**
	 * 
	 * ftp文件上传
	 * @see com.libra.service.FtpService#uploadFtp(java.lang.String)
	 */
	@Override
	public ResponseData uploadFtp(String context) {
		//生成的文件名称
		String fileName = sdf.format(new Date());
		boolean flag = false;
		//列出所有windows的磁盘
		File[] fs = File.listRoots();
		for(int i = 0; i <fs.length; i ++) {
//    	   File file = new File(fs[i].toString());
			FtpResponse ftpResponse =createCsv(fs[i].toString(),fileName,context);
   			//FTP上传
   			flag = FtpUtils.uploadFile(FtpConstans.FTP_HOST, FtpConstans.FTP_USER_NAME, FtpConstans.FTP_PASSWORD, 
   					FtpConstans.FTP_PORT, FtpConstans.FTP_PATH, ftpResponse.getLocalName(),ftpResponse.getLocalUrl());
   			if(flag) {
   				return ResponseData.success(ftpResponse);
   			}
	    }
		return ResponseData.error("文件上传失败");
	}
	/**
	 * 
	 * createCsv:(在本地磁盘生成txt文档). <br/>
	 * @author wangzhch
	 * @param filePath
	 * @param fileName
	 * @param context
	 * @return
	 * @since JDK 1.8
	 */
	private FtpResponse createCsv(String filePath, String fileName, String context) {
		//这里的path是指磁盘下的文件夹
		String path = filePath+"测试";
		File file = new File(path);
		if(!file.isDirectory()) {
			file.mkdirs();
		}
		String localPath = path+File.separator+fileName+".txt";
		FileWriter fileWriter;
		try {
			fileWriter = new FileWriter(localPath,true);//true在文中追加
			fileWriter.write(context);
			fileWriter.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		log.info("写入路径：{}",localPath);
		FtpResponse ftpResponse = new FtpResponse();
		ftpResponse.setLocalName(fileName);
		ftpResponse.setLocalUrl(localPath);
		return ftpResponse;
	}
	/**
	 * 
	 * Ftp文件下载
	 * @see com.libra.service.FtpService#downLoadFtp()
	 */
	@Override
	public ResponseData downLoadFtp(String downName) {
		String localUrl = PathUtil.getWebRootPath()+"ftpDown";
		boolean flag = FtpUtils.downloadFtpFile(FtpConstans.FTP_HOST, FtpConstans.FTP_USER_NAME, FtpConstans.FTP_PASSWORD, 
   					FtpConstans.FTP_PORT, FtpConstans.FTP_PATH,downName, localUrl);
		if(flag) {
			FtpResponse ftpResponse = new FtpResponse();
			ftpResponse.setLocalName(downName);
			ftpResponse.setLocalUrl(localUrl);
			return ResponseData.success(ftpResponse);
		}
		return ResponseData.error("文件下载失败");
	}

}

